package com.iflytek.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.iflytek.dto.UserDTO;
import com.iflytek.entity.User;
import com.iflytek.service.UserService;
import com.iflytek.util.BeanUtil;
import com.iflytek.util.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class HandleFileController {

    @Autowired
    private UserService userService;

    //上传
    @PostMapping("/upload")
    public String uploadFile(MultipartFile file) {

        try {
            EasyExcel.read(file.getInputStream(), UserDTO.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                    UserDTO userDTO = (UserDTO) o;
                    System.out.println(userDTO);
                    User user = new User();
                    BeanUtils.copyProperties(userDTO, user);
                    userService.save(user);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "redirect:queryUser";
    }

    //下载 ResponseEntity<byte[]>
    @GetMapping("/downloadUsers")
    public void downloadUsers(Integer[] ids, HttpServletResponse response) throws Exception {
        //构建一个正常的响应对象，当前这个对象是空的 返回 ResponseEntity<byte[]> 需要找到
//        ResponseEntity.BodyBuilder builder = ResponseEntity.ok();

        //通知浏览器以下载方式打开文件，并指定文件名
//        builder.header("Content-Disposition", "attachment;filename="
//                + URLEncoder.encode("用户导入模板.xlsx", "UTF-8"));
        //通知浏览器以流的形式返回文件数据，浏览器会执行下载操作
//        builder.contentType(MediaType.APPLICATION_OCTET_STREAM);

        //将文件内容读取成byte[]
//        byte[] content = FileUtils.readFileToByteArray(new File("E:\\Program Files\\liaohq\\ideaProject\\users.xlsx"));

        //用文件内容填充ResponseEntity对象，并返回给浏览器
//        return builder.body(content);

        //easyExcel写文件
        String fileName = "用户文档";
        String sheetName = "用户文档";
        //获取全部选择的用户信息
        List<User> userList;
        List<UserDTO> userDTOS;
        if (ids != null) {
            userList = userService.listByIds(Arrays.asList(ids));
            userDTOS = BeanUtil.copyList(userList, UserDTO.class);
        } else {
            userList = userService.list();
            userDTOS = BeanUtil.copyList(userList, UserDTO.class);
        }

        try {
            //userDTO.class对应你的模板类
            //userDTOS模板的例子
            //也可以使用这种方式导出你查询出数据excel文件
            ExcelUtil.writeExcel(response, userDTOS, fileName, sheetName, UserDTO.class);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

}
